﻿<cfscript>
/** 
* @hint "Entity: ExamPlan, ExamTask, ExamCell"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.taskExamListener" 
	output=false
	depends="sequenceExamTask,sessionAdvice,logFactory" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "更新考试场次的起止时间"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateExamCell( required MachII.framework.Event event ) output=false {
		
		var cellId = event.getArg("CellIDX");
		
		var sql = "";
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			
			sql = "UPDATE t_examcell a
					SET a.time_start = :TimeStart ,
						a.time_end = :TimeEnd 
					WHERE a.cellidx = :cellId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="TimeStart", value= replace(event.getArg("TimeStart"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TimeEnd", value= replace(event.getArg("TimeEnd"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cellId", value=cellId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
		}
		
	}
	
	
	/**
	* @hint "更新考试起止时间"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateNewExamTask( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("taskId");
		
		var sql = "";
		var queryObj = ""; 
		
		var tchId = getsessionAdvice().getAutherUserID();
		var content = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT tch_name FROM t_teacher where tch_id = :teacherId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value= tchId , cfsqltype="cf_sql_varchar" );
			
			rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			sql = "SELECT a.tsk_id,a.tch_id,a.term_id, b.tch_name, a.cid,  c.course_name 
					FROM t_task a 
					INNER JOIN t_teacher b ON b.tch_id = a.tch_id 
					INNER JOIN t_course c ON c.cid = a.cid 
					WHERE a.tsk_id = :taskId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			sql = "UPDATE t_new_examtask set time_start = :TimeStart, time_end = :TimeEnd, is_arranged = '3' where task_id = :taskId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="TimeStart", value= replace(event.getArg("TimeStart"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TimeEnd", value= replace(event.getArg("TimeEnd"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			content = rs_teacher.tch_name & " 添加 考试任务 " & rs_task.course_name & "-" & rs_task.tch_name;
			
			logFactory.stuCourse('11', content, rs_task.tch_id, rs_task.cid, rs_task.term_id);
			
		}
		
	}
	
	
	/**
	* @hint "更新提前考试起止时间"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateNewExamAdviceTask( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("taskId");
		
		var sql = "";
		var queryObj = ""; 
		
		var tchId = getsessionAdvice().getAutherUserID();
		var content = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT tch_name FROM t_teacher where tch_id = :teacherId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value= tchId , cfsqltype="cf_sql_varchar" );
			
			rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			sql = "SELECT a.tsk_id,a.tch_id,a.term_id, b.tch_name, a.cid,  c.course_name 
					FROM t_task a 
					INNER JOIN t_teacher b ON b.tch_id = a.tch_id 
					INNER JOIN t_course c ON c.cid = a.cid 
					WHERE a.tsk_id = :taskId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			
			sql = "UPDATE t_new_examtaskAdvice set time_start = :TimeStart, time_end = :TimeEnd, is_arranged = '3' where task_id = :taskId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="TimeStart", value= replace(event.getArg("TimeStart"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TimeEnd", value= replace(event.getArg("TimeEnd"), ":", "", "ALL") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			content = rs_teacher.tch_name & " 添加 提前考试任务 " & rs_task.course_name & "-" & rs_task.tch_name;
			
			logFactory.stuCourse('11', content, rs_task.tch_id, rs_task.cid, rs_task.term_id);
			
			var passArgs = structNew();
			structInsert( passArgs, "TabID", "labDoneArrange", true );

			event.setArgs( passArgs );
			
		}
		
	}
	
	/**
	* @hint "删除考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteNewExamTask( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("taskId");
		
		var sql = "";
		var queryObj = ""; 
		
		var tchId = getsessionAdvice().getAutherUserID();
		var content = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT tch_name FROM t_teacher where tch_id = :teacherId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value= tchId , cfsqltype="cf_sql_varchar" );
			
			rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			sql = "SELECT a.tsk_id,a.tch_id,a.term_id, b.tch_name, a.cid,  c.course_name 
					FROM t_task a 
					INNER JOIN t_teacher b ON b.tch_id = a.tch_id 
					INNER JOIN t_course c ON c.cid = a.cid 
					WHERE a.tsk_id = :taskId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			
			sql = "DELETE FROM t_new_examtask where task_id = :taskId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			content = rs_teacher.tch_name & " 删除 考试任务 " & rs_task.course_name & "-" & rs_task.tch_name;
			
			logFactory.stuCourse('11', content, rs_task.tch_id, rs_task.cid, rs_task.term_id);
			
		}
		
	}
	
	/**
	* @hint "删除提前考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteNewExamAdviceTask( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("taskId");
		
		var sql = "";
		var queryObj = ""; 
		
		var tchId = getsessionAdvice().getAutherUserID();
		var content = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT tch_name FROM t_teacher where tch_id = :teacherId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value= tchId , cfsqltype="cf_sql_varchar" );
			
			rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			sql = "SELECT a.tsk_id,a.tch_id,a.term_id, b.tch_name, a.cid, c.course_name 
					FROM t_task a 
					INNER JOIN t_teacher b ON b.tch_id = a.tch_id 
					INNER JOIN t_course c ON c.cid = a.cid 
					WHERE a.tsk_id = :taskId ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			
			sql = "DELETE FROM t_new_examtaskAdvice where task_id = :taskId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			sql = "DELETE FROM t_new_examAdvice_student where task_id = :taskId ";
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value= taskId , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			content = rs_teacher.tch_name & " 删除 提前考试任务 " & rs_task.course_name & "-" & rs_task.tch_name;
			
			logFactory.stuCourse('11', content, rs_task.tch_id, rs_task.cid, rs_task.term_id);
			
		}
		
	}
	
	/**
	* @hint "标志课程的指定教学班需参加期末考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function restoreExamTaskClass( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TaskID");
		
		var sql = "";
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT task_type FROM t_examplan WHERE task_id = :taskId "; 
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
			sql = "UPDATE t_examplan SET is_arrange = '0', exidx = :examIndex WHERE task_id = :taskId "; 
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
						
		}
		
	}
	
	/**
	* @hint "标志课程的指定教学班不参加期末考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamTaskClass( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TaskID");
		var sql = ""; 
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT task_type FROM t_examplan WHERE task_id = :taskId "; 
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
			sql = "UPDATE t_examplan SET is_arrange = 'N', exidx = :examIndex WHERE task_id = :taskId "; 
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
		}
		
	}

	/**
	* @hint "标志课程需参加期末考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function restoreExamCourse( required MachII.framework.Event event ) output=false {
		
		var courseId = event.getArg("CourseID");
		var termId = event.getArg("TermID");
		var taskType = event.getArg("TaskType");
		
		var sql = ""; 
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT 
						task_id 
					FROM 
						t_examplan 
					WHERE 
						cid = :courseId 
						AND 
						term_id = :termId 
						AND 
						task_type = :taskType 
						AND 
						is_arrange = 'N' 
					ORDER BY 
						task_id ";
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
			
			rs_plan = queryObj.execute( sql=sql ).getResult(); 
		
			for ( var i=1; i LTE rs_plan.recordCount; i++){
				
				sql = "SELECT task_type FROM t_examplan WHERE task_id = :taskId "; 
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_examplan SET is_arrange = '0', exidx = :examIndex WHERE task_id = :taskId "; 
							
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
			}
					
		}

	}
	
	/**
	* @hint "标志课程不参加期末考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamCourse( required MachII.framework.Event event ) output=false {
		
		var courseId = event.getArg("CourseID");
		var termId = event.getArg("TermID");
		var taskType = event.getArg("TaskType");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT 
						task_id 
					FROM 
						t_examplan 
					WHERE 
						cid = :courseId 
						AND 
						term_id = :termId 
						AND 
						task_type = :taskType 
						AND 
						is_arrange = '0' 
					ORDER BY 
						task_id ";
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
			
			rs_plan = queryObj.execute( sql=sql ).getResult(); 
			
			for ( var i=1; i LTE rs_plan.recordCount; i++){
				
				sql = "SELECT task_type FROM t_examplan WHERE task_id = :taskId "; 
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_examplan 
						SET is_arrange = 'N', 
							exidx = :examIndex 
						WHERE 
							task_id = :taskId "; 
							
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
			}
		}

	}
	
	/**
	* @hint "删除考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamTask( required MachII.framework.Event event ) output=false {
		
		var taskExid = event.getArg("TaskExamID");
		
		var sql = "";
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT cid, term_id, exidx FROM t_examtask WHERE task_exid = :taskExid "; 
			
			queryObj = new query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			
			var rs_examTask = queryObj.execute( sql=sql ).getResult(); 
			
			sql = "SELECT task_id FROM t_examplan 
					WHERE cid = :courseId 
						  AND 
						  term_id = :termId 
						  AND 
						  exidx = :examIndex "; 
			
			queryObj = new query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="courseId", value=rs_examTask.cid, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=rs_examTask.term_id, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examIndex", value=rs_examTask.exidx, cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult(); 
			
			for ( i=1; i LTE rs_plan.recordCount; i++){
				
				sql = "SELECT task_type FROM t_examplan WHERE task_id = :taskId "; 
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_examplan SET is_arrange = '0', exidx = :examIndex WHERE task_id = :taskId "; 
							
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
							
			}
			
			sql = "DELETE FROM t_examtask WHERE task_exid = :taskExid "; 
				
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
			
		}
	}
	
	/**
	* @hint "创建考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function createExamTask( required MachII.framework.Event event ) output=false {
		
		var sequenceExamTask = getsequenceExamTask(); 
		
		var taskIds = event.getArg("TaskClassIDs");
		var examWeek = event.getArg("WID");
		var termId = event.getArg("TermID");
		var courseId = event.getArg("CourseID");
		var campusId = event.getArg("CampusID");
				
		var sql = "";
		var queryObj = ""; 
				
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			
			/* 选课且已缴费的学生 */
			sql = "SELECT COUNT(*) countStu FROM t_student_course 
					WHERE task_id IN (:taskIds ) AND SUBSTR(choose_prop, 2, 1) = '1' "; 
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskIds", value=taskIds, cfsqltype="cf_sql_varchar", list=true, separator="|" );
			
			var rs_chooserCount = queryObj.execute( sql=sql ).getResult(); 
		
		

			/* 使用已选人数代替考试计划人数 */
			/* 生成考序号 */
			sequenceExamTask.setTermID(termId);
			sequenceExamTask.setExamType("F");
			var examIdx = sequenceExamTask.getExamIDX();
			
			/* 生成考试任务号 */
			sequenceExamTask.setExamIndex(examIdx);
			var examTaskId = sequenceExamTask.getID();

			sql = "INSERT INTO t_examtask(
								task_exid,
								term_id,
								cid,
								exidx,
								wid,
								wdidx,
								cellidx,
								task_prog,
								campus_id,
								exam_type,
								examer_sum
							)VALUES(
								:taskExamID,
								:termId, 
								:courseId, 
								:examIndex, 
								:examWeek, 
								:examWeekDay, 
								:cellIndex, 
								:taskProg, 
								:campusId, 
								:examType, 
								:examSize 
							)";
							
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskExamID", value=examTaskId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="examIndex", value=examIdx, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examWeek", value=examWeek, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examWeekDay", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cellIndex", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskProg", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examType", value="F", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examSize", value=rs_chooserCount.countStu, cfsqltype="cf_sql_integer" );
			
			queryObj.execute( sql=sql );
			
		
			/* 标注考试计划未已安排考试任务 */
			for ( var taskId in listToArray(taskIds, "|") ) {
				
				sql = "UPDATE t_examplan SET is_arrange = '1', exidx = :examIndex WHERE task_id = :taskId ";
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=examIdx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
			}

		}

	}

	/**
	* @hint "创建期中考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function createMidExamTask( required MachII.framework.Event event ) output=false {
		
		var sequenceExamTask = getsequenceExamTask(); 
		
		var taskIds = event.getArg("TaskClassIDs");
		var examWeek = event.getArg("WID");		
		var taskType = event.getArg("TaskType");	
		var termId = event.getArg("TermID");
		var courseId = event.getArg("CourseID");
		var campusId = event.getArg("CampusID");
		
		var sql = ""; 
		var queryObj = "";
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 选课且已缴费的学生 */
			sql = "SELECT COUNT(*) countStu FROM t_student_course 
					WHERE task_id IN (:taskIds ) AND SUBSTR(choose_prop, 2, 1) = '1' "; 
					
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskIds", value=taskIds, cfsqltype="cf_sql_varchar", list=true, separator="|" );
			
			var rs_chooserCount = queryObj.execute( sql=sql ).getResult(); 


			/* 使用已选人数代替考试计划人数 */
			
			/* 生成考序号 */
			sequenceExamTask.setTermID(termId);
			sequenceExamTask.setExamType("M");
			var examIdx = sequenceExamTask.getExamIDX();
			
			/* 生成考试任务号 */
			sequenceExamTask.setExamIndex(examIdx);
			var examTaskId = sequenceExamTask.getID();
			
			sql = "INSERT INTO t_examtask(
								task_exid,
								term_id,
								cid,
								exidx,
								wid,
								wdidx,
								cellidx,
								task_prog,
								campus_id,
								exam_type,
								examer_sum
							)VALUES(
								:taskExamID,
								:termId, 
								:courseId, 
								:examIndex, 
								:examWeek, 
								:examWeekDay, 
								:cellIndex, 
								:taskProg, 
								:campusId, 
								:examType, 
								:examSize 
							)";
							
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskExamID", value=examTaskId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="examIndex", value=examIdx, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examWeek", value=examWeek, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examWeekDay", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cellIndex", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskProg", value="0", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examType", value="M", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examSize", value=rs_chooserCount.countStu, cfsqltype="cf_sql_integer" );
			
			queryObj.execute( sql=sql );
			
			/* 标注考试计划已安排考试任务 */
			for ( var taskId in listToArray(taskIds, "|") ) {
				
				sql = "UPDATE t_exammidplan SET is_arrange = '1', exidx = :examIndex WHERE task_id = :taskId ";
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=examIdx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
			}

		}

	}

	/**
	* @hint "创建期中考试考试计划"
	* 
	* @event "MachII Event Object"
	*/
	public void function createMidExamPlan( required MachII.framework.Event event ) output=false {

		var courseId = event.getArg("CID");
		var termId = event.getArg("TID");
		
		var sql = ""; 
		var queryObj = "";

		/* 生成期中考试计划 */ 
		sql = "SELECT a.tsk_id, a.cidx FROM  t_task a WHERE a.term_id = :termId AND a.cid = :courseId ";
		
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
		
		var rs_programTask = queryObj.execute( sql=sql ).getResult();
		
	
		sql = "SELECT a.tsk_id, a.cidx FROM  t_common_task a WHERE a.term_id = :termId AND a.cid = :courseId ";
		
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
		
		var rs_commonTask = queryObj.execute( sql=sql ).getResult();
		
		
		sql = "SELECT a.tsk_id, a.cidx FROM  t_reopen_task a WHERE a.term_id = :termId AND a.cid = :courseId ";
		
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
		
		var rs_openTask = queryObj.execute( sql=sql ).getResult();
		
		transaction {
			
			if( rs_programTask.recordCount ){

				for ( var i=1; i LTE rs_programTask.recordCount; i++){

					sql = "SELECT 1 FROM t_exammidplan WHERE task_id = :taskId ";
					
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="taskId", value=rs_programTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
					
					var rs_planEntity = queryObj.execute( sql=sql ).getResult();

					if(rs_planEntity.recordCount EQ 0){
						
						sql = "INSERT INTO t_exammidplan(
									task_id, task_type, term_id, cid, exidx, is_arrange 
								)VALUES(
									:taskId, :taskType, :termId, :courseId, :examIndex, :isArrange 
								)";
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="taskId", value=rs_programTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="taskType", value="P", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="examIndex", value="P00000", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="isArrange", value="0", cfsqltype="cf_sql_char" );
						
						queryObj.execute( sql=sql );

					}
				}
			}
			
			if( rs_commonTask.recordCount ){
				
				for ( i=1; i LTE rs_commonTask.recordCount; i++){
					
					sql = "SELECT 1 FROM t_exammidplan WHERE task_id = :taskId ";
					
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="taskId", value=rs_commonTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
					
					var rs_planEntity = queryObj.execute( sql=sql ).getResult();
					
					if(rs_planEntity.recordCount EQ 0){
						
						sql = "INSERT INTO t_exammidplan(
									task_id, task_type, term_id, cid, exidx, is_arrange 
								)VALUES(
									:taskId, :taskType, :termId, :courseId, :examIndex, :isArrange 
								)";
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="taskId", value=rs_commonTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="taskType", value="C", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="examIndex", value="C00000", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="isArrange", value="0", cfsqltype="cf_sql_char" );
						
						queryObj.execute( sql=sql );

					}
							
				}
			}
			

			if(rs_openTask.recordCount){
				
				for ( i=1; i LTE rs_openTask.recordCount; i++){
					
					sql = "SELECT 1 FROM t_exammidplan WHERE task_id = :taskId ";
					
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="taskId", value=rs_openTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
					
					var rs_planEntity = queryObj.execute( sql=sql ).getResult();

					if(rs_planEntity.recordCount EQ 0){
						
						sql = "INSERT INTO t_exammidplan(
									task_id, task_type, term_id, cid, exidx, is_arrange 
								)VALUES(
									:taskId, :taskType, :termId, :courseId, :examIndex, :isArrange 
								)";
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="taskId", value=rs_openTask["tsk_id"][i], cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="taskType", value="R", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="examIndex", value="R00000", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="isArrange", value="0", cfsqltype="cf_sql_char" );
						
						queryObj.execute( sql=sql );

					}
					
				}
			}
			
		}
	}
	
	/**
	* @hint "期中考试取消考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamMidCourse( required MachII.framework.Event event ) output=false {
		
		var courseId = event.getArg("CourseID");
		var termId = event.getArg("TermID");
		
		var sql = "";
		var queryObj = "";
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT task_id FROM t_exammidplan 
					WHERE cid = :courseId 
						  AND 
						  term_id = :termId 
						  AND 
						  is_arrange = :isArrange 
					ORDER BY task_id "; 
			
			queryObj = new query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="isArrange", value='0', cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult(); 
			
			for ( var i=1; i LTE rs_plan.recordCount; i++){
				
				sql = "DELETE FROM t_exammidplan WHERE task_id = :taskId "; 
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );

			}
		}

	}
	
	/**
	* @hint "标志课程的指定教学班不参加期中考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamMidTaskClass( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TaskID");
		
		var sql = ""; 
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT task_type FROM t_exammidplan WHERE task_id = :taskId "; 
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
			sql = "UPDATE t_exammidplan SET is_arrange = 'N', exidx = :examIndex WHERE task_id = :taskId "; 
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );

		}

	}
	
	/**
	* @hint "标志课程的指定教学班需参加期中考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function restoreExamMidTaskClass( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TaskID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT task_type FROM t_exammidplan WHERE task_id = :taskId "; 
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
			sql = "UPDATE t_exammidplan SET is_arrange = '0', exidx = :examIndex WHERE task_id = :taskId "; 
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );
			
		}
		
	}
	
	/**
	* @hint "删除期中考试任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamMidTask( required MachII.framework.Event event ) output=false {
		
		var taskExid = event.getArg("TaskExamID");
		
		var sql = ""; 
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT cid, term_id, exidx FROM t_examtask WHERE task_exid = :taskExid "; 
			
			queryObj = new query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			
			var rs_examTask = queryObj.execute( sql=sql ).getResult(); 
			
			sql = "SELECT task_id FROM t_exammidplan 
					WHERE cid = :courseId 
						  AND 
						  term_id = :termId 
						  AND 
						  exidx = :examIndex "; 
			
			queryObj = new query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="courseId", value=rs_examTask.cid, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=rs_examTask.term_id, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examIndex", value=rs_examTask.exidx, cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult(); 
		
			
			for ( var i=1; i LTE rs_plan.recordCount; i++){
				
				sql = "SELECT task_type FROM t_exammidplan WHERE task_id = :taskId "; 
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_exammidplan SET is_arrange = '0', exidx = :examIndex WHERE task_id = :taskId "; 
							
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="examIndex", value=rs_planEntity.task_type & "00000", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=rs_plan["task_id"][i], cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
			}
			sql = "DELETE FROM t_examtask WHERE task_exid = :taskExid "; 
				
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
		}
	}
	
}
</cfscript>